<?php
require(dirname(__FILE__).'/../lib/curl.php');
class MediaTempleGPUMonitor {
    var $req;
    
    function MediaTempleGPUMonitor($session=false) {
        $this->req = new TanTanCurl();
        if ($session) {
            $this->setSession($session);
        }
    }
    
    function isLoggedIn() {
        return $this->loggedin ? true : false;
    }
    function login($domain, $email, $password) {
        $this->req->setMethod(HTTP_REQUEST_METHOD_POST);
        $this->req->setURL('https://ac.mediatemple.net/login.mt');
        $this->req->addPostData('domain', $domain);
        $this->req->addPostData('login_email', $email);
        $this->req->addPostData('password', $password);
        $this->req->addPostData('rememberme', '1');
        $this->req->addPostData('login', 'login');
        $this->req->addPostData('redirect', '/home.mt');

        $this->req->sendRequest();
        $cookies = $this->req->getResponseCookies();
        $body = $this->req->getResponseBody();
        $headers = $this->req->getResponseHeader();
        $this->setSession($cookies);
        
        $this->req->setMethod(HTTP_REQUEST_METHOD_GET);
        //$this->req->clearPostData();
        $this->req->setURL('https://ac.mediatemple.net/check_cookies.mt');
        $this->req->sendRequest();
        $cookies = $this->req->getResponseCookies();
        $body = $this->req->getResponseBody();
        $headers = $this->req->getResponseHeader();
             
        $this->req->setMethod(HTTP_REQUEST_METHOD_POST);
        $this->req->setURL('https://ac.mediatemple.net/login.mt');
        $this->req->sendRequest();
        $cookies = $this->req->getResponseCookies();
        $body = $this->req->getResponseBody();
        $headers = $this->req->getResponseHeader();

        if ($cookies['mt_ac_primary_domain']) {
            $this->setSession($cookies);
            return true;
        }
        
        return false;
    }
    function logout() {
        $this->loggedin = false;
    }    
    function getSession() {
        return $this->req->cookies;
    }
    function setSession($cookies) {
        if (!is_array($cookies)) {
            return false;
        }
        foreach ($cookies as $c) {
            $this->req->addCookie($c['name'], $c['value']);
        }
        $this->loggedin = true;
        return true;
    }    
    function getCurrentGPUData($server) {
        $this->req->setMethod(HTTP_REQUEST_METHOD_GET);
        $this->req->setURL('https://ac.mediatemple.net/services/manage/grid/reports/gpu/current.mt?id='.$server);
        $this->req->sendRequest();
        $cookies = $this->req->getResponseCookies();
        $body = $this->req->getResponseBody();
        $headers = $this->req->getResponseHeader();        
//        print_r($headers);
        return $this->_parseGPUPage($body);
    }
    
    function _parseGPUPage($html) {
        
        $start = '<!-- InstanceBeginEditable name="content_body" -->';
        $end = '<!-- InstanceEndEditable -->';
        $work = ereg_replace(".*$start(.*)$end.*", "\\1", $html);
        $work = strip_tags($work, '<tr><td>');
        $work = preg_replace('/\s\s+/', ' ', $work);
        //$work = preg_replace('/&[a-z]+;/', '', $work);
        $work = preg_split('/<\/tr>|<tr[^>]*>/', $work, -1, PREG_SPLIT_NO_EMPTY);
        foreach ($work as $line) {
            $line = trim($line);
            if (ereg('<td', $line)) {
                $col = preg_split('/<\/td>|<td[^>]*>/', ($line), -1, PREG_SPLIT_NO_EMPTY);
                foreach ($col as $k => $c) {
                    if (!trim($c)) unset($col[$k]);
                }
                if (count($col) >= 5) {
                    $new = array(); // reset keys
                    foreach ($col as $c) {
                        $new[] = $c;
                    }
                    $cols[] = $new;
                }
            }
        }
        
        return $cols[1][1];
    }
    
}
